/*
 * Copyright(C) 2019 FUYUN DATA SERVICES CO.,LTD. - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * 该源代码版权归属福韵数据服务有限公司所有
 * 未经授权，任何人不得复制、泄露、转载、使用，否则将视为侵权
 *
 */

import { isArray } from 'util';

/**
 * Copyright(C) 2019 FUYUN DATA SERVICES CO., LTD. - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 */

// import Authority from '../config/authority';

// 设置权限
export const setAuthority = (authority: string | string[]) => {
  localStorage.setItem('user_authority', JSON.stringify(authority));
};

// 类型保护,如果用户没有设置权限，则默认为admin
function f(sn: string | null): string {
  return sn || JSON.stringify('');
}
// 获取权限
export const getAuthority = () => {
  // 类型保护 如果没有设置权限，则默认为admin
  return JSON.parse(f(localStorage.getItem('user_authority')));
};

// 重置权限
export const resetAuthority = () => {
  localStorage.removeItem('user_authority');
};

// 判断权限
export const judgeAuthority = (authority: string | string[]) => {
  let userAuthority: string | string[] = getAuthority();
  let isPass = false;
  if (userAuthority) {
    if (!isArray(userAuthority)) {
      userAuthority = userAuthority.split(',');
    }
    if (!isArray(authority)) {
      authority = authority.split(',');
    }
    if (isArray(userAuthority) && isArray(authority)) {
      userAuthority.forEach(item => {
        for (const ele of authority) {
          if (item === ele) {
            isPass = true;
          }
        }
      });
    }
  }
  return isPass;
};
